#coding:utf-8
"""
*   TG_Coder : Dzlua
*   Email    : 505544956@qq.com
*   Time     : 2017/09/14
"""

import codecs
import string, sys, os
import threadpool

class Mklib:
    def __init__(self, bin_path, dst_path):
        self._libdir = dst_path
        self._libbin = bin_path
    def write(self, text):
        f = open('mklib.log', 'a')
        f.write(text)
        f.close()
    def is_tar_gz(self, file):
        if file.rfind('.tar.gz') == -1:
            return False
        return True
    def walk(self, path, outfiles):
        for (root,dirs,files) in os.walk(path):
            for file in files:
                tar_gz = root + '/' + file
                if self.is_tar_gz(tar_gz):
                    outfiles.append(tar_gz)
                    print(tar_gz)
            for dir in dirs:
                self.walk(root + dir, outfiles)
    def unpack(self, file):
        dir,name,type = self._filedir(file, 2)
        dst = dir + '/' + name

        #del dir
        self._deldirfile(dst)
        #create dir
        self._mkdirs(dst, True)
        #untar
        cmd = 'tar -zxf %s -C %s' % (file, dst)
        print '[Mklib] unpack tar : %s' % cmd
        ret = ( os.system(cmd) == 0 )
        assert(ret)

        print '[Mklib] unpack tar : done!'

        return dst,name,type
    def mklib(self, sourdir, filename):
        '''> colemake mem sourcedir libname libdir '''
        cmd = '%s mem %s %s %s' % (self._libbin, sourdir, filename, self._libdir)
        print '[Mklib] mklib %s : %s' % (filename, cmd)
        ret = ( os.system(cmd) == 0 )
        assert(ret)
        self._deldirfile(sourdir)

        print '[Mklib] mklib %s : done!' % filename
    def _filedir(self, file, times=1):
        pos = file.rfind('/')
        dir = file[0 : pos]
        name = file[pos : len(file)]
        while times > 0:
            pos = name.rfind('.', 0, pos)
            times -= 1
        return dir, name[len('/') : pos], name[pos : len(name)]
    def _deldirfile(self, path):
        try:
            cmd = 'rm -rf %s' % path
            os.system(cmd)
        except:
            print('[Mklib] deldirfile error,(?): %s' % path)
    def _mkdirs(self, path, dir=False):
        pt = path
        try:
            if dir:
                os.makedirs(path)
                return
            pt = path[0 : path.rfind('/')]
            os.makedirs(pt)
        except:
            print('[Mklib] mkdirs error,(exist?): %s' % pt)
#--------------------#
#path = '/home/zhouhan/dzlua/tmp'
#dst_path = '/home/zhouhan/dzlua/tmp/tar'
path = '/disk2/ftp/data'
dst_path = '/disk2/coledata/colelib/it'
bin_path = '/cole2/colemake'
#
threads = 4
tasks = 4
#----------#
def callback(mklib, file, allcount, curcount):
    try:
        print('[Mklib] ----------')
        print('[Mklib] %s/%s' % (curcount, allcount))
        dir,name,type = mklib.unpack(file)
        mklib.mklib(dir, name)
    except:
        errmsg = '[Mklib] ERROR : %s' % (file)
        mklib.write(errmsg + '\n')
        print(errmsg)
#----------#
def tar_gz_name(file):
    epos = file.rfind('.tar.gz')
    if epos == -1:
        return None
    bpos = file.rfind('/', 0, epos)
    if bpos == -1:
        bpos = 0
    
    return file[bpos + 1 : epos]
#----------#
def done_files(dfiles, dst_path):
    for (_root,_dirs,_files) in os.walk(dst_path):
        for _dir in _dirs:
            dfiles.append(_dir)
        break
#----------#
def check_files(files, dfiles):
    def has_file_name(name):
        for dfile in dfiles:
            if dfile == name:
                return True
        return False
    #

    ndfiles = []
    for file in files:
        name = tar_gz_name(file)
        if name is not None:
            if has_file_name(name) is False:
                ndfiles.append(file)
    return ndfiles
#----------#
def put(pool, fun, mklib, file, allcount, curcount):
    def _fun(args):
        return fun(mklib, file, allcount, curcount)
    reqs = threadpool.makeRequests(_fun, [''])  
    [ pool.putRequest(req) for req in reqs ]
    print('[Mklib] put fun : %s/%s, %s' % (curcount, allcount, file))
#----------#
mklib = Mklib(bin_path, dst_path)
files = []
mklib.walk(path, files)
#
dfiles = []
done_files(dfiles, dst_path)
#
files = check_files(files, dfiles)
#
pool = threadpool.ThreadPool(threads, tasks)
#
curcount = 0
allcount = len(files)
print('[Mklib] files count : %s' % allcount)
for file in files:
    curcount += 1
    put(pool, callback, mklib, file, allcount, curcount)
#
pool.wait()
#----------#
